Skip to main content

mv — Move & Rename Files/Directories Linux coreutils

mv (move) is the “relocation” command on Linux. It can rename a file (same folder, new name) or move it into a new directory.
On a VPS, mv is used constantly for deployments (swap new builds), backups (archive old configs), log rotations, and even WordPress operations like disabling plugins by renaming their folders.

Think like a sysadmin

If cp is “make a copy,” mv is “change where it lives.”
Renaming is just moving within the same directory.

Overwrite risk

If the destination already exists, mv can overwrite it. Use -i or -n when safety matters.

Prerequisites

  • Linux VPS (Ubuntu recommended)
  • SSH access
  • Familiarity with WordPress paths like /var/www/html/, /backups/

Verify mv exists (GNU coreutils)

command -v mv

Expected output (example):

/usr/bin/mv

Check version:

mv --version

Expected output (example):

mv (GNU coreutils) 9.4

Verify cp exists (used in examples)

command -v cp
cp --version

Syntax (complete)

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Notes:

  • If DEST is an existing directory, sources are moved into DEST/.
  • If DEST is not a directory, SOURCE is renamed/moved to the DEST path.
  • If the destination path already exists, mv may overwrite it unless you use a safety flag.

Options That Matter

OptionMeaningTypical Use
-iInteractive: prompt before overwriteProtect configs like wp-config.php
-nNo overwrite (no-clobber)Safe automation, preserve backups
-fForce overwrite (no prompt)CI/CD deployments (use carefully)
-uMove only if source is newerRefresh updated theme assets
-vVerbose: show each actionLogs/debugging
-t DIRMove sources into DIRScripts; avoids DEST ambiguity
-TTreat DEST as a normal fileAvoid moving into an existing dir
-b, --backup[=CONTROL]Backup overwritten destinationSafer overwrites with rollback
--End of optionsMove names starting with -
--helpShow helpQuick reference
--versionShow versionCompatibility checks
Recommended defaults
  • Manual work: prefer -i
  • Automation: prefer -n (or controlled -f with logging)
  • Unsure what DEST is: add -v and confirm with ls -ld DEST

Common Patterns

mv index.php index-old.php

Best Practices

  • For manual work on important files, prefer mv -iv ... (prompt + visibility).
  • For scripts, prefer mv -n ... when you must never overwrite.
  • Confirm what DEST is before moving: ls -ld DEST
  • If overwriting is expected, keep a rollback copy: mv -bv new-config.php wp-config.php
  • When moving many sources, prefer -t and --: mv -t /backups -- file1 file2
  • Need a copy instead of a move? Use cp (examples use it for backups).
WordPress trick: disable a plugin quickly

Renaming a plugin folder usually disables it because WordPress can’t find its entry point.

Troubleshooting

ProblemLikely CauseFix
File not movedWrong pathCheck with ls and use absolute paths
Permission deniedOwned by root / restricted pathUse sudo mv ... or fix ownership
File overwrittenNo safety flag usedUse -i (prompt) or -n (skip)
Unexpected rename vs moveDestination wasn’t a directoryConfirm with ls -ld DEST
Quick verification commands (HTML)
# Confirm current directory
pwd

# Confirm what DEST is
ls -ld DEST

# Check result
ls -lah /path/to/target

Cheat Sheet

mv SOURCE DEST # Rename/move one path
mv SRC1 SRC2 DIR/ # Move many sources into DIR/
mv -t DIR SRC1 SRC2 # Same, safer for scripting
mv -iv SOURCE DEST # Prompt + verbose (good manual default)
mv -n SOURCE DEST # Never overwrite
mv -bv new-config.php wp-config.php # Overwrite but keep wp-config.php~
mv -- -file ./dest/ # Move a name starting with '-'
mv -T SOURCE DEST # Treat DEST as a normal file

Mini Quiz

  1. What does mv -i do when a file already exists at the destination?
  2. Which flag prevents overwriting existing files?
  3. How would you disable a plugin without logging into WP admin using mv?
  4. Which option shows each file being moved?
Answers (peek after you try)
  1. Prompts before overwriting.
  2. -n
  3. Rename the plugin folder (e.g., old-plugin -> old-plugin-disabled).
  4. -v

Worked Examples (with expected output)

Structure note

Examples come after the concepts so the flags make sense when you see them in action.

Rename a file

mv index.php index-old.php

Expected output: (silent success)

Move a file to another directory

mv wp-config.php /backups/

Expected output: (silent success)

Move and rename at the same time

mv debug.log /backups/debug-2025.log

Expected output: (silent success)

Move multiple files into a directory

mv file1.php file2.php /var/www/html/wp-content/

Expected output: (silent success)

Verbose mode

mv -v style.css /var/www/html/wp-content/themes/mytheme/

Expected output:

renamed 'style.css' -> '/var/www/html/wp-content/themes/mytheme/style.css'

Interactive overwrite

mv -i wp-config.php /backups/

Expected output (if file exists at destination):

mv: overwrite '/backups/wp-config.php'?

Force overwrite (automation-style)

mv -f new-config.php wp-config.php

Expected output: (silent success)

Use carefully

This can replace critical configs without warning.

No overwrite

mv -n wp-config.php /backups/

Expected output: (silent success; skips if destination exists)

Move only if newer (-u)

mv -u new-style.css /var/www/html/wp-content/themes/mytheme/

Expected output: (silent success if source is newer)

Move a directory

mv wp-content/uploads/2024 wp-content/old_uploads/

Expected output: (silent success)

Rename a directory (disable a plugin)

mv wp-content/plugins/old-plugin wp-content/plugins/old-plugin-disabled

Expected output: (silent success)

Move with wildcard

mv *.log /var/log/wp/

Expected output: (silent success)

Preview wildcards first
ls *.log

Move into /tmp/

mv test.php /tmp/

Expected output: (silent success)

Move multiple directories with brace expansion

mv {themes,plugins} /backups/wp-content/

Expected output: (silent success)

Keep a copy (workaround)

cp index.php index-copy.php && mv index.php archive/

Expected output: (silent success)

Rename logs by date

mv debug.log debug-$(date +%Y-%m-%d).log

Expected output: (silent success)

Move with sudo

sudo mv wp-config.php /etc/wordpress/

Expected output: (silent success)

Replace existing file with verbose + force

mv -vf new-index.php index.php

Expected output:

renamed 'new-index.php' -> 'index.php'

Move folder contents (keep the folder)

mv wp-content/cache/* wp-content/old_cache/

Expected output: (silent success)

Bulk move using find

find . -name "*.bak" -exec mv -v {} backups/ \;

Expected output (example):

renamed './wp-config.php.bak' -> 'backups/wp-config.php.bak'

Move an entire WordPress site directory (advanced)

mv -v /var/www/html /var/www/old_html

Expected output:

renamed '/var/www/html' -> '/var/www/old_html'
Service impact

Moving the web root can break your live site unless your web server config is updated.